<?php
namespace app\Admin\controller;
use app\common\controller\AdminBase;
class Auth extends AdminBase{
    protected $authList = array(
        'admin/system/shielding'        =>  '敏感词',
        'admin/system/shielding_add'    =>  '添加敏感词',
        'admin/system/shielding_edit'   =>  '编辑敏感词',
        'admin/system/shielding_delete' =>  '删除敏感词',
        'admin/system/shielding_config' =>  '敏感词替换',
        'admin/system/publish'          =>  '发布敏感词',
        'admin/system/log'              =>  '系统日志',
        'admin/system/user_export'      =>  '导出用户',
        'admin/system/log_export'       =>  '导出日志',
        'admin/system/clearcache'       =>  '清理缓存',
        'admin/user/index'              =>  '管理员列表',
        'admin/user/adduser'            =>  '添加管理员页面',
        'admin/user/adduserhandle'      =>  '添加管理员请求',
        'admin/user/userlock'           =>  '锁定管理员',
        'admin/user/gruplock'           =>  '锁定用户组',
        'admin/user/deluser'            =>  '删除管理员',
        'admin/user/updateuser'         =>  '修改管理员',
        'admin/user/grouplist'          =>  '用户组列表',
        'admin/user/groupauth'          =>  '配置用户组权限',
        'admin/user/groupsethandle'     =>  '更新用户组权限',
        'admin/user/updategroup'        =>  '更新用户组',
        'admin/user/delgroup'           =>  '删除用户组',
        'admin/user/addgroup'           =>  '添加用户组页面',
        'admin/user/addgrouphandle'     =>  '添加用户组请求',
        'admin/auth/authlist'           =>  '权限列表',
        'admin/auth/rulelock'           =>  '禁用权限',
        'admin/auth/addauth'            =>  '添加权限页面',
        'admin/auth/addauthhandle'      =>  '添加权限请求',
        'admin/auth/deleterule'         =>  '删除权限',
        'admin/auth/updaterule'         =>  '修改权限',
        'admin/auth/authcatelist'       =>  '权限分类列表',
        'admin/auth/addauthcate'        =>  '添加权限分类页面',
        'admin/auth/addauthcatehandle'  =>  '添加权限分类请求',
        'admin/auth/deletecate'         =>  '删除分类',
        'admin/auth/updatecate'         =>  '修改分类',
        'admin/stream/index'            =>  '直播间列表',
        'admin/stream/info'             =>  '直播间详情',
        'admin/stream/gettmpeaccount'   =>  '直播间实时统计',
        'admin/stream/video_config'     =>  '视频回放配置',
        'admin/stream/stream_config'    =>  '直播配置',
        'admin/stream/rm_upload_image'  =>  '删除默认封面',
        'admin/stream/setdisable'       =>  '设置禁播状态',
        'admin/stream/getdisabletime'   =>  '计算禁播时间',
        'admin/stream/getstreamusername'=>  '获取用户昵称',
        'admin/stream/sendmessage'      =>  '发送IM消息',
        'admin/stream/sendendstream'    =>  '发送禁播通知',
        'admin/gift/gift'               =>  '礼物配置',
        'admin/gift/gift_add'           =>  '添加礼物',
        'admin/gift/do_add_gift'        =>  '添加礼物配置',
        'admin/gift/rand_gift_code'     =>  '礼物编号',
        'admin/gift/create_gift_code'   =>  '生成礼物编号',
        'admin/gift/rm_gift_image'      =>  '删除礼物图片',
        'admin/gift/gift_edit'          =>  '编辑礼物',
        'admin/gift/do_edit_gift'       =>  '处理编辑礼物',
        'admin/filter/index'            =>  '视频过滤配置',
        'admin/filter/add'              =>  '添加过滤配置',
        'admin/stream/edit_notice'      =>  '直播间公告',
        'admin/stream/webim_config'     =>  '聊天服务器配置',
        'admin/syncdata/index'          =>  '直播用户对接',
        'admin/video/index'             =>  '回放视频列表',
        'admin/video/delvideo'          =>  '删除回放视频',
        'admin/msgtext/addmessage'      =>  '发送站内信',
        'admin/msgtext/index'           =>  '发出的站内信',
        'admin/msgtext/recmessage'      =>  '收到的站内信',
    );
	//获取权限规则列表
    public function authList(){
        $m=D('RuleView');
        $data = $m->order('id')->findPage(10);
        $this->assign('data',$data['data']);
        $this->assign('page',$data);
        return $this->fetch();
    }
    //锁定解锁规则
    public function ruleLock(){
        $user=M('auth_rule');
        $data['id']=decrypt_id($this->id);
        $data['status']=$this->lock;
        if($user->save($data)){
            //记录操作日志
            $content = '锁定解锁规则';
            $type = 'admin_user';
            $ext = array('key'=>'admin_auth_authList');
            system_log($content,$type,$ext);
            return ['status' => 1, 'data' => ['info'=>'操作成功','jumpurl' => U('admin/Auth/authList')]];
        }else{
            return ['status'=>0,'message'=>'操作失败'];
        }
    }
    //添加后台权限
    public function addAuth(){
		$modules=M('modules');
		$modules=$modules->select();
        $authList = M('auth_rule')->field('name')->select();
        $authLists = getSubByKey($authList,'name');
        $configAuthList = array_flip($this->authList);
        $diff =  array_flip(array_diff($configAuthList,$authLists));
        if(empty($diff))
        {
            $no_data = '没有权限可以添加了!!!';
            $this->assign('no_data',$no_data);
        }
        $this->assign('authlist',$diff);
		$this->assign('modules',$modules);
        return $this->fetch();
    }
    //后台权限表单处理
    public function addAuthHandle(){
    	if(IS_POST){
            $data=array(
                'name'=>strtolower(string_filter($this->name)),
                'title'=>string_filter($this->title),
                'status'=>intval($this->status) ? $this->status : 0,
                'condition'=>string_filter($this->condition),
                'mid'=>intval($this->modules)?$this->modules:'',
                );
            if(!$data['name'])
            {
                 return ['status'=>0,'message'=>'权限路径为空,添加失败'];
            }
            if(!$data['title'])
            {
                 return ['status'=>0,'message'=>'权限名称为空,添加失败'];
            }
            $authTitleLen = mb_strlen($data['title'],'UTF8');
            if($authTitleLen <1 || $authTitleLen > 20)
            {
                return ['status'=>0,'message'=>'权限名称字数在1-20之间'];
            }
            if(!$data['mid'])
            {
                  return ['status'=>0,'message'=>'所属模块为空,添加失败'];
            }
            $old = M('auth_rule')->where(array('name'=>$data['name']))->find();
            if($old)
            {
               return ['status'=>0,'message'=>'该权限已经存在,添加失败'];
            }
            if(M("auth_rule")->add($data)){
                // 记录操作日志
                $content = '添加权限';
                $type = 'admin_auth';
                $ext = array('key'=>'admin_auth_addAuthHandle');
                system_log($content,$type,$ext);
                 return ['status' => 1, 'data' => ['info'=>'添加成功','jumpurl' => U('admin/Auth/authList')]];
            }else{
                 return ['status'=>0,'message'=>'添加失败'];
            }
        }else{
             return ['status'=>0,'message'=>'添加失败'];
        }
    }
    //删除RULE
    public function deleteRule(){
        if(IS_POST)
        {
            $id=decrypt_id($this->id);
            if(!$id){
                return ['status'=>0,'message'=>'操作失败'];
            }
            if(M("auth_rule")->where(array("id"=>$id))->delete()){
                // 记录操作日志
                $content = '删除权限节点';
                $type = 'admin_auth';
                $ext = array('key'=>'admin_auth_deleteRule');
                system_log($content,$type,$ext);
                return ['status' => 1, 'data' => ['info'=>'删除成功','jumpurl' => U('admin/Auth/AuthList')]];
            }else{
                 return ['status'=>0,'message'=>'删除失败'];
            }
        }
    }
    //修改RULE
    public function updateRule(){
        if(IS_POST)
        {
            $id = decrypt_id($this->id);
            if(!$id){
                return ['status'=>0,'message'=>'当前规则不存在'];
            }
            $data = array(
                'id'=>$id,
                'title'=>string_filter($this->title),
                'status'=>intval($this->status),
                'mid'=>intval($this->modulename),
                'condition'=>''
            );
            $ruleData = M('auth_rule')->where(array('id'=>$id))->find();
            if(!array_diff($data,$ruleData))
            {
                return ['status'=>0,'message'=>'没做任何修改'];
            }
            if(M("auth_rule")->where(array("id"=>$data['id']))->save($data))
            {
                // 记录操作日志
                $content = '修改权限节点';
                $type = 'admin_auth';
                $ext = array('key'=>'admin_auth_updateRule');
                system_log($content,$type,$ext);
                 return ['status' => 1, 'data' => ['info'=>'修改成功','jumpurl' => U('admin/Auth/AuthList')]];
            }else{
                return ['status'=>0,'message'=>'修改失败'];
            }
        }else{
            $rule['id']=$where['id']=decrypt_id($this->id);
            $rule=D('RuleView');
            $rule=$rule->where($where)->select();
            $this->assign('rule',$rule);
            $modules=M('modules');
            $modules=$modules->select();
            $this->assign('modules',$modules);
             return $this->fetch();
        }
    }

/*后台权限分类操作——————————————————————————————————————————————————————————————————————————————————*/

    //权限分类列表
    public function authCateList(){
        $modules=M("modules")->order('id asc')->findPage(15);
        $this->assign('modules',$modules['data']);
        $this->assign('page',$modules);
        return $this->fetch();
    }
    //添加权限分类
    public function addAuthCate(){
        return $this->fetch();
    }
    //处理权限分类添加
    public function addAuthCateHandle(){
        if(IS_POST)
        {
            $data['modulename']=$this->modulename;
            $modules=M('modules');
            if($data['modulename'] == null)
            {
                 return ['status'=>0,'message'=>'分类名称不能为空'];
            }else{
                $moduleNameLen = mb_strlen($data['modulename'],'UTF8');
                if($moduleNameLen < 1 || $moduleNameLen > 20)
                {
                    return ['status'=>0,'message'=>'分类名称在1-20位之间'];
                }
                if($modules->add($data))
                {
                    // 记录操作日志
                    $content = '添加权限分类';
                    $type = 'admin_auth';
                    $ext = array('key'=>'admin_auth_addAuthCateHandle');
                    system_log($content,$type,$ext);
                     return ['status' => 1, 'data' => ['info'=>'添加成功','jumpurl' => U('admin/Auth/authCateList')]];
                }else{
                     return ['status'=>0,'message'=>'添加失败'];
                }
            }
        }else{
            return ['status'=>0,'message'=>'添加失败'];
        }
    }

    //删除分类
    public function deleteCate()
    {
        if(IS_POST)
        {
            $id=decrypt_id($this->id);
            if(!$id){
                return ['status'=>0,'message'=>'操作失败'];
            }
            if(M("modules")->where(array("id"=>$id))->delete()){
                //需要删除rule表的相应rule
                M('auth_rule')->where(array('mid'=>$id))->delete();
                // 记录操作日志
                $content = '删除权限分类';
                $type = 'admin_auth';
                $ext = array('key'=>'admin_auth_deleteCate');
                system_log($content,$type,$ext);
                return ['status' => 1, 'data' => ['info'=>'删除成功','jumpurl' => U('admin/Auth/authCateList')]];
            }else{
                 return ['status'=>0,'message'=>'删除失败'];
            }
        }
    }

    //修改分类
    public function updateCate(){
        if(IS_POST){
            $data['modulename'] = string_filter($this->modulename);
            $data['id']=decrypt_id($this->id);
            if(!$data['id'])
            {
                return ['status'=>0,'message'=>'操作失败'];
            }
            if($data['modulename'])
            {
                $modulenameLen = mb_strlen($data['modulename'],'UTF8');
                if($modulenameLen < 1 || $modulenameLen > 20)
                {
                    return ['status'=>0,'message'=>'分类名称在1-20位之间'];
                }
                $modulesData = M('modules')->where(array('modulename'=>$data['modulename']))->find();
                if($modulesData['id'] === $data['id'])
                {
                    return ['status'=>0,'message'=>'没做任何修改'];
                }elseif ($modulesData)
                {
                    return ['status'=>0,'message'=>'该分类已经存在'];
                }
            }
            if(M('modules')->save($data))
            {
                // 记录操作日志
                $content = '修改权限分类';
                $type = 'admin_auth';
                $ext = array('key'=>'admin_auth_updateCate');
                system_log($content,$type,$ext);
                return ['status' => 1, 'data' => ['info'=>'修改成功','jumpurl' => U('admin/Auth/authCateList')]];
            }
        }else{
            $id=decrypt_id($this->id);
            if(!$id)
            {
                return ['status'=>0,'message'=>'操作失败'];
            }
            $modules=M("modules");
            $modules=$modules->where(array('id'=>$id))->select();
            $this->assign('modules',$modules);
            return $this->fetch();
        }
    }
}